package lab2.gl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class ApplicationAssociation {

	private static File myFile = new File("fisierCompanii.txt");

	public static void write_Object(Serializable object, String fileName)
			throws FileNotFoundException, IOException {
		ObjectOutputStream myStream = new ObjectOutputStream(
				new FileOutputStream(fileName));
		myStream.writeObject(object);
		myStream.close();
	}

	public static Object load_Object(String fileName)
			throws FileNotFoundException, IOException, ClassNotFoundException {
		ObjectInputStream input = new ObjectInputStream(new FileInputStream(
				fileName));
		Object myObj = input.readObject();
		input.close();
		return myObj;
	}

	public static void createCompanie() throws FileNotFoundException,
			IOException {

		CompanieAssurance compa = new CompanieAssurance("Compania mea");
		Personne pers1 = new Personne("Client1");
		Personne pers2 = new Personne("Client2");
		Personne pers3 = new Personne("Client3");
		compa.addClients(pers1);
		compa.addClients(pers2);
		compa.addClients(pers3);
		write_Object(compa, myFile.getName());
	}

	public static void displayCompanie() throws IOException,
			ClassNotFoundException {
		CompanieAssurance writeCompanie = (CompanieAssurance) load_Object(myFile.getName());
		writeCompanie.display();
	}

	public static void main(String[] args) throws FileNotFoundException,
			IOException, ClassNotFoundException {
		createCompanie();
		createCompanie();
		displayCompanie();
		displayCompanie();
	}

}
